﻿using System;
using System.Collections.Generic;
using System.Linq;
using Altairis.FluffyCloud.Data;
using Altairis.Web;

namespace Altairis.FluffyCloud.WebCore.Pages.Forum {

    public partial class Acl : System.Web.UI.Page {

        public void AclEntriesList_DeleteItem(Guid aclEntryId) {
            var topicId = int.Parse(this.RouteData.Values["id"] as string);
            using (var dc = new FluffyDbContext()) {
                var entry = dc.TopicAclEntries.FirstOrDefault(x => x.TopicId == topicId && x.AclEntryId == aclEntryId);
                if (entry == null) return;
                dc.TopicAclEntries.Remove(entry);
                dc.SaveChanges();
                this.Response.Redirect(this.Request.Url.ToString());
            }
        }

        public IEnumerable<TopicAclEntry> AclEntriesList_GetData() {
            var topicId = int.Parse(this.RouteData.Values["id"] as string);

            using (var dc = new FluffyDbContext()) {
                var topic = dc.Topics.SingleOrDefault(x => x.TopicId == topicId);
                if (topic == null) {
                    // Topic not found
                    this.Response.SendErrorResponse(404, "Object Not Found", "Topic not found");
                    return null;
                }
                if (!System.Web.Security.Roles.IsUserInRole(Role.AdministratorsRoleName) && !topic.UserName.Equals(this.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase)) {
                    // Neither owner or admin
                    this.Response.SendErrorResponse(401, "Unauthorized", "You must be forum owner od administrator");
                    return null;
                }
                var q = from e in dc.TopicAclEntries.Include("Role")
                        where e.TopicId == topicId
                        orderby e.Operation, e.Action
                        select e;
                return q.ToArray();
            }
        }

        public void AclEntriesList_InsertItem(TopicAclEntry item) {
            item.TopicId = int.Parse(this.RouteData.Values["id"] as string);
            using (var dc = new FluffyDbContext()) {
                dc.TopicAclEntries.Add(item);
                dc.SaveChanges();
                this.Response.Redirect(this.Request.Url.ToString());
            }
        }

        public IEnumerable<object> RoleList_GetData() {
            using (var dc = new FluffyDbContext()) {
                var q = from r in dc.Roles
                        orderby r.RoleName
                        select new {
                            Text = r.RoleName + " (" + r.Description + ")",
                            Value = r.RoleName
                        };
                return q.ToArray();
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
        }

        protected void ResetButton_Click(object sender, EventArgs e) {
            var topicId = int.Parse(this.RouteData.Values["id"] as string);
            using (var dc = new FluffyDbContext()) {
                var entriesToDelete = dc.TopicAclEntries.Where(x => x.TopicId == topicId).ToList();
                foreach (var entry in entriesToDelete) {
                    dc.TopicAclEntries.Remove(entry);
                }
                dc.TopicAclEntries.Add(new TopicAclEntry { TopicId = topicId, RoleName = Role.EveryoneRoleName, Operation = TopicAclEntry.OperationRead, Action = true });
                dc.TopicAclEntries.Add(new TopicAclEntry { TopicId = topicId, RoleName = Role.EveryoneRoleName, Operation = TopicAclEntry.OperationPost, Action = true });
                dc.SaveChanges();
                this.Response.Redirect(this.Request.Url.ToString());
            }
        }
    }
}